﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;
using System.Xml;

namespace WebConfigModifier
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack<string> argStack = new Stack<string>(args.Reverse());

            string value = null;
            if (IsConsoleRedirected())
            {
                value = Console.In.ReadToEnd();
                value = value.Trim(" \"\t\r\n".ToCharArray());
            }

            try
            {
                new WebConfigUpdater().HandleRequest(argStack, value);
            }
            catch (Exception)
            {
                Environment.Exit(1);
            }
        }

        /// <summary>
        /// Returns true if the stdin has been redirected from a file or pipe; false otherwise.
        /// </summary>
        private static bool IsConsoleRedirected()
        {
            try
            {
                bool dummy = Console.KeyAvailable;
                return false;
            }
            catch (InvalidOperationException)
            {
                // swallow. We actually *want* this behaviour - it occurs when stdin has been redirected from another
                // program - but there's no way to test for this without using PInvoke. It's my Saturday morning. No.
                return true;
            }
        }

    }
}
